#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
//全局变量
int high,width;//游戏画面尺寸
int ball_x,ball_y;//小球的坐标
float ball_vx,ball_vy;//小球速度
int position_x,position_y;//挡板的中心坐标
int radius;//挡板的半径
int left,right;//挡板的左右边界
int bullet_x,bullet_y;
int ball_number;
int ny=5;
int score;
void HideCursor()//隐藏光标
{
	CONSOLE_CURSOR_INFO cursor_info={1,0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void gotoxy(int x,int y)//光标移动到(x,y)
{
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(handle,pos);
}

void startup()//数据初始化
{
	high=16;
	width=25;
	ball_y=width/2;
	ball_x=1;
	ball_vx=1;
	ball_vy=1;
	position_x=high;
	position_y=width/2;
	radius=5;
	left=position_y-radius;
	right=position_y+radius;
	ball_number=0;
	bullet_x=0;
	bullet_y=position_y;
	score=0;


	HideCursor();//隐藏光标
	
}
void show()//显示画面
{
	gotoxy(0,0);//光标移到原点
	int i,j;
	for(i=0;i<=high+1;i++)
	{
		for(j=0;j<=width;j++)
		{
			if((i==ball_x)&&(j==ball_y))
				printf("*");//输出小球
			else if((i==bullet_x)&&(j==bullet_y))
				printf("B");
			else if(j==width)
				printf("|");//输出右边界
			else if(i==high+1)
				printf("-");//输出下边界
			else if((i==high)&&(j>=left)&&(j<=right))
				printf("*");//输出挡板
		else
			printf(" ");
		}
		printf("\n");
	}
	printf("反弹小球数：%d\n",ball_number);
	printf("得分:%d\n",score);
}
void updateWithoutInput()//与用户输入无关的数据
{
	if((bullet_x==ball_x)&&(bullet_y==ball_y))
	{
		score++;
	printf("%d",score);
	}
	if(ball_x==high-1)
	{
		if((ball_y>=left)&&(ball_y<=right))
		{
			ball_number++;
			ball_vy=-ball_vy;
		}
		else
		{
			printf("Game Over!");
			exit(0);
		}
	}
	//根据速度更新小球的位置
	ball_x=ball_x+ball_vx;
	ball_y=ball_y+ball_vy;
	//碰到边界后，改变速度的方向，实现反弹
	if((ball_x==0)||(ball_x==high-1))
		ball_vx=-ball_vx;
	if((ball_y==0)||(ball_y==width-1))
		ball_vy=-ball_vy;
	Sleep(50);
}

	

void updateWithInput()
{
	char input;
	if(kbhit())
	{
		input=getch();
		if(input=='a')
		{
			position_y--;
			left=position_y-radius;
	        right=position_y+radius;
		}
		if(input=='d')
		{
			position_y++;
			left=position_y-radius;
	        right=position_y+radius;
		}
	}

}
int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
}

  